<?php

namespace app\admin\controller\zhiye;

use app\admin\controller\Controller;
use app\admin\model\zhiye\Company as CompanyModel;
use app\admin\model\article\Category as CategoryModel;
use think\Db;
use think\Session;

/**
 * 销售人员控制器
 */
class Company extends Controller{
    public function index(){
        $model = new CompanyModel;
        $companyId=Session::get('yoshop_adminuser')['user']['company_id'];
        $list = $model->getList($this->request->get(),$companyId);
        return $this->fetch('index', compact('list'));
    }
    /**
     * 获取项目组楼盘信息
     */

    /**
     * 添加人员
     */
    public function add(){
        $model = new CompanyModel;
        $ajax=input('post.proid');
        if($ajax){
            $pro=Db::name('yonghui_building')->where(['projectId'=>$ajax])->field('id,name,projectId')->select();
            return json(['code'=>1,'data'=>$pro]);
        }
        if(empty($this->postData('company')['name'])){
            //获取当前登录公司的项目组信息
            $projectInfo=Db::name('yonghui_project')->where(['companyId'=>Session::get('yoshop_adminuser')['user']['company_id']])->select();
            //获取当前登录公司的所有驻场人员
            $zhuchang=Db::name('yonghui_user')->where(['companyId'=>Session::get('yoshop_adminuser')['user']['company_id'],'type'=>1])->select()->toArray();
            if($zhuchang){
                foreach($zhuchang as $k=>$v){
                    if($v['projectId']){
                        $zhuchang[$k]['zhuchangName']=Db::name('yonghui_project')->where(['id'=>$v['projectId']])->field('id,name')->find()['name'];
                    }
                }
            }
            //获取渠道总监信息
            $zonjianInfo=Db::name('yonghui_user')->where(['companyId'=>Session::get('yoshop_adminuser')['user']['company_id'],'type'=>5])->select();
            //获取总经理信息
            $zonjingliInfo=Db::name('yonghui_user')->where(['companyId'=>Session::get('yoshop_adminuser')['user']['company_id'],'type'=>7])->select();
            return $this->fetch('add',compact('projectInfo','zhuchang','zonjianInfo','zonjingliInfo'));
        }
        $data=$this->postData('company');
        if($data['type']==1&&empty($data['projectId'])){
            return $this->renderError($model->getError() ?: '项目组为必选');
        }
        if($data['type']==1&&empty($data['loupanids'])){
            return $this->renderError($model->getError() ?: '楼盘为必选');
        }
        if($data['type']==2&&empty($data['zhuchangId'])){
            return $this->renderError($model->getError() ?: '驻场人员为必选');
        }
        if($data['type']==4&&empty($data['zonjianId'])){
            return $this->renderError($model->getError() ?: '所属总监为必选');
        }
        if($data['type']==5&&empty($data['zonjingliId'])){
            return $this->renderError($model->getError() ?: '所属总经理为必选');
        }
        if(!preg_match("/^1[345789]\d{9}$/", $data['phone'])) {
            return $this->renderError($model->getError() ?: '手机号格式不正确');
        }
        //判断该手机号已添加过
        $userInfo=Db::name('yonghui_user')->where(['phone'=>$data['phone']])->find();
        if($userInfo){
            return $this->renderError($model->getError() ?: '该手机号已添加过');
        }
        if($data['password']!=$data['repassword']){
            return $this->renderError($model->getError() ?: '两次输入的密码不一致，请重新输入');
        }
        //判断驻场人员与项目组绑定
        if($data['type']==1){
            /*if(in_array('all',$data['loupanids'])){
                $data['loupanids']='all';
            }else{
                $data['loupanids']=implode(',', $data['loupanids']);
            }*/
            //判断绑定的楼盘信息
            $result=Db::name('yonghui_user')->where(['projectId'=>$data['projectId'],'type'=>1,'loupanids'=>$data['loupanids']])->find();
            if($result){
                return $this->renderError($model->getError() ?: '该楼盘已绑定驻场人员');
            }
        }
        $data['upuserid']=0;
        if($data['type']==1){
            $projectId=$this->postData('company')['projectId'];
        }elseif ($data['type']==2){
            $results=Db::name('yonghui_user')->where(['id'=>$data['zhuchangId'],'type'=>1])->field('id,type,projectId,loupanids')->find();
            $projectId=$results['projectId'];
            $data['loupanids']=$results['loupanids'];
        }else{
            $projectId=0;
            if($data['type']==4){
                $data['upuserid']=$data['zonjianId'];
            }
            if($data['type']==5){
                $data['upuserid']=$data['zonjingliId'];
            }
        }
        //所属公司id
        $data['companyId']=Session::get('yoshop_adminuser')['user']['company_id'];
        $data['createTime']=time();
        $data['password']=md5($data['password']);
        $data['status']=1;
        $data['projectId']=$projectId;
        $insert=$model->add($data);
        if($insert){
            return $this->renderSuccess('添加成功', url('zhiye.company/index'));
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 更新人员
     */
    public function edit($id){
        //人员详情
        $model = CompanyModel::detail($id);
        $model['zhuchangId']=Db::name('yonghui_user')->where(['projectId'=>$model['projectId'],'type'=>1])->field('id,name')->find()['id'];
        if (!$this->request->isAjax()){
            $projectInfo=Db::name('yonghui_project')->where(['companyId'=>Session::get('yoshop_adminuser')['user']['company_id']])->select();
            //获取当前登录公司的所有驻场人员
            $zhuchang=Db::name('yonghui_user')->where(['companyId'=>Session::get('yoshop_adminuser')['user']['company_id'],'type'=>1])->select()->toArray();
            if($zhuchang){
                foreach($zhuchang as $k=>$v){
                    if($v['projectId']){
                        $zhuchang[$k]['zhuchangName']=Db::name('yonghui_project')->where(['id'=>$v['projectId']])->field('id,name')->find()['name'];
                        $zhuchang[$k]['zhuchangId']=Db::name('yonghui_user')->where(['projectId'=>$v['projectId'],'type'=>1])->field('id,type,projectId,name')->find()['id'];
                    }
                }
            }
            return $this->fetch('edit', compact('model','projectInfo','zhuchang'));
        }
        //判断该手机号已添加过
        $data=$this->postData('company');
        $userInfo=Db::name('yonghui_user')->where(['phone'=>$data['phone']])->find();
        if($userInfo&&$id!=$userInfo['id']){
            return $this->renderError($model->getError() ?: '该手机号已添加过');
        }
        //判断驻场人员与项目组绑定
        /*if($model['type']==1){
            $result=Db::name('yonghui_user')->where(['projectId'=>$data['projectId'],'type'=>1])->find();
            if($result && $result['id']!=$id){
                return $this->renderError($model->getError() ?: '该项目组已绑定驻场人员');
            }
        }
        if($model['type']==1&&empty($data['projectId'])){
            return $this->renderError($model->getError() ?: '项目组为必选');
        }
        if($model['type']==2&&empty($data['zhuchangId'])){
            return $this->renderError($model->getError() ?: '驻场人员为必选');
        }
        if($model['type']==1){
            $projectId=$data['projectId'];
        }else{
            $results=Db::name('yonghui_user')->where(['id'=>$data['zhuchangId'],'type'=>1])->field('id,type,projectId')->find();
            $projectId=$results['projectId'];
        }
        $data['projectId']=$projectId;
        unset($data['zhuchangId']);*/
        if (!preg_match("/^1[345789]\d{9}$/", $this->postData('company')['phone'])) {
            return $this->renderError($model->getError() ?: '手机号格式不正确');
        }

        $data['updateTime']=time();
        if ($model->edit($data)) {
            return $this->renderSuccess('更新成功', url('zhiye.company/index'));
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 删除人员
     */
    public function delete($id){
        $model = CompanyModel::detail($id);
        if (!$model->setDelete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }
    /*
     * 恢复
     */
    public function recover($id){
        $model = CompanyModel::detail($id);
        if (!$model->recover()) {
            return $this->renderError($model->getError() ?: '恢复失败');
        }
        return $this->renderSuccess('恢复成功');
    }
    /**
     * 添加渠道经理指标
     */
    public function adddata($id){
        if(!$this->request->isAjax()){
            //获取楼盘数据
            $projectInfo=Db::name('yonghui_building')->where(['companyId'=>Session::get('yoshop_adminuser')['user']['company_id'],'status'=>1])->field('id,name')->select()->toArray();
            return $this->fetch('adddata',compact('projectInfo','id'));
        }
        //判断该渠道经理 该楼盘  该年份  该月份指标是否已经添加
        $where=$this->postData('company');
        $where['companyId']=Session::get('yoshop_adminuser')['user']['company_id'];
        $where['createTime']=time();
        $swhere['loupanid']=$where['loupanid'];
        $swhere['year']=$where['year'];
        $swhere['month']=$where['month'];
        $swhere['userId']=$where['userId'];
        $info=Db::name('yonghui_month')->where($swhere)->find();
        if($info){
            return $this->renderError('当前月份配置数据已存在');
        }
        $res=Db::name('yonghui_month')->insertGetId($where);
        if($res){
            return $this->renderSuccess('添加成功','/index.php?s=admin/zhiye.company/viewdata&id='.$swhere['userId']);
        }
        return $this->renderError('添加失败');
    }
    /**
     * 查看渠道经理指标
     */
    public function viewdata($id){
        //获取配置数据
        $list=Db::name('yonghui_month')->where(['companyId'=>Session::get('yoshop_adminuser')['user']['company_id'],'userId'=>$id])->paginate(15, false, [
            'query' => request()->request()
        ]);
        return $this->fetch('viewdata',compact('list'));
    }
    /**
     * 删除渠道经理指标数据
     */
    public function deldata($id){
         $del=Db::name('yonghui_month')->where(['id'=>$id])->delete();
         if($del){
             return $this->renderSuccess('删除成功','/index.php?s=admin/zhiye.company/index');
         }
        return $this->renderError('删除失败');
    }
}